<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        欢迎登录 冠记系统
      </div>
      <!-- 登录表单区域 -->
      <el-form ref="loginForm" :model="form" :rules="rules" label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="form.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password" style="margin-bottom: 10px;">
          <el-input v-model="form.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
        </el-form-item>
        <div>
          <el-button type="text" @click="modal = true" style="margin-left: 10px;">修改密码</el-button>
          <!-- 按钮区域 -->
          <el-form-item class="btns">
            <el-button type="primary" @click='login'>登录</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <el-dialog
      title="修改密码"
      :visible.sync="modal"
      width="30%">
      <el-form :model="formChangepw" status-icon :rules="ruleChange" ref="formChangepw" label-width="100px" class="demo-ruleForm">
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formChangepw.username"></el-input>
        </el-form-item>
        <el-form-item label="当前密码" prop="passwd">
          <el-input type="password" v-model="formChangepw.passwd" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPasswd">
          <el-input type="password" v-model="formChangepw.newPasswd" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="passwdCheck">
          <el-input type="password" v-model="formChangepw.passwdCheck" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="ok">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      // 这是登录表单的数据绑定对象
      form: {
        username: '',
        password: ''
      },
      // 这是表单的验证规则对象
      rules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' }
        ]
      },
      modal: false,
      formChangepw: {
        passwd: '',
        passwdCheck: '',
        newPasswd: '',
        username: ''
      },
      ruleChange: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        passwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        newPasswd: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        passwdCheck: [
          { required: true, message: '请输入再次确认密码', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return
        const { data: result } = await this.$http.post('/login/token', this.form)
        if (result.status !== 200) return this.$message.error(result.msg)
        this.$message.success('登录成功')
        // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中
        //   1.1 项目中出了登录之外的其他API接口，必须在登录之后才能访问
        //   1.2 token 只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage 中
        localStorage.setItem('token', result.data.access_token)
        localStorage.setItem('gj_access', JSON.stringify(result.data.access))
        localStorage.setItem('userid', JSON.stringify(result.data.userid))
        localStorage.setItem('time', new Date().getTime())
        // 2. 通过编程式导航跳转到后台主页，路由地址是 /home
        this.$router.push('/home')
      })
    },
    cancel() {
      this.formChangepw = {
        passwd: '',
        passwdCheck: '',
        newPasswd: '',
        username: ''
      }
      this.modal = false
    },
    ok() {
      const data = {
        passwd: this.formChangepw.passwd,
        passwdCheck: this.formChangepw.passwdCheck,
        newPasswd: this.formChangepw.newPasswd,
        username: this.formChangepw.username
      }
      this.$refs.formChangepw.validate(async valid => {
        if (!valid) return
        const { data: result } = await this.$http.post('/change/passwd', data)
        if (result.status !== 200) return this.$message.error(result.msg)
        this.$message.success('保存成功')
        this.cancel()
      })
    },
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 250px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    font-size: 20px;
    margin: 10px;
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  float: right;
}
.changePw {
  margin-bottom: 5px;
}
</style>
